#include<iostream>
#include<string>
#include<set>
#include<list>
#include<algorithm>

using namespace std;


//list就是双向循环链表

//list容器构造
/*
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int> L1;

	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历容器
	printList(L1);

	//区间方式构造
	list<int> L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int> L3(L1);
	printList(L3);

}

int main()
{
	test01();
	return 0;
}
*/




/*
//list容器赋值和交换
//赋值
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int> L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	list<int>L2;
	L2 = L1;	//operator= 赋值
	printList(L2);

	list<int> L3;
	L3.assign(L2.begin(), L2.end());	//assign赋值
	printList(L3);

	list<int> L4;
	L4.assign(10, 100);
	printList(L4);
}

//交换
void test02()
{
	list<int> L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	list<int>L2;
	L2.assign(10, 100);
	printList(L2);

	cout << "交换后: " << endl;
	L1.swap(L2);
	printList(L1);
	printList(L2);

}

int main()
{
	test01();
	cout << endl;
	test02();
	return 0;
}
*/





/*
//list容器大小操作
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	printList(L1);

	//判断容器是否为空
	if (L1.empty())
	{
		cout << "容器为空。" << endl;
	} 
	else
	{
		cout << "L1不为空。" << endl;
		cout << "L1的元素个数为：" << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10, 11);
	printList(L1);

	L1.resize(2);
	printList(L1);
}

int main()
{
	test01();
	return 0;
}
*/





/*
//list容器 插入和删除
//与deque相似，多了一个remove
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int> L1;

	//尾插
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);

	//头插
	L1.push_front(40);
	L1.push_front(50);
	L1.push_front(60);

	printList(L1);

	//尾删
	L1.pop_back();
	printList(L1);

	//头删
	L1.pop_front();
	printList(L1);

	//insert插入
	list<int>::iterator it = L1.begin();
	it++;
	it++;
	L1.insert(it, 1000);	//在迭代器部位插入元素1000
	printList(L1);

	//删除
	it = L1.begin();
	L1.erase(it);
	printList(L1);

	//移除
	L1.push_back(10000);
	L1.push_back(10000);
	printList(L1);
	L1.remove(10000);	//删除容器中所有10000
	printList(L1);

	//清空
	L1.clear();
	printList(L1);
}
int main()
{
	test01();
	return 0;
}
*/





/*
//list容器数据存取
//list不支持[]和at方式访问，不支持随机访问
//list.back()和list.front() 返回第一个元素和最后一个元素
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);

	cout << "第一个元素为: " << L1.front() << endl;
	cout << "最后一个元素为: " << L1.back() << endl;

}
int main()
{
	test01();
	return 0;
}
*/






/*
//list容器反转和排序
//list.reverse()	//反转链表
//list.sort()		//链表排序
void printList(const list<int>& L1)
{
	for (list<int>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

bool myCompare(int v1, int v2)
{
	//降序，第一个数大于第二个数
	return v1 > v2;
}

void test01()
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(30);
	L1.push_back(20);
	L1.push_back(40);
	printList(L1);

	//反转
	L1.reverse();
	cout << "反转后: " << endl;
	printList(L1);

	//排序
	//所有不支持随机访问的迭代器不能使用标准sort(.begin(), .end())算法排序
	//内部会提供算法
	L1.sort();		//默认升序
	cout << "排序后: " << endl;
	printList(L1);

	//降序
	L1.sort(myCompare);
	printList(L1);
}

int main()
{
	test01();
	return 0;
}
*/






//list容器——排序案例
//将Person自定义数据类型进行排序，有姓名，年龄，身高
//按照年龄进行升序，若年龄相同，按照身高降序

class Person
{
public:
	Person(string name, int age, int hight)
	{
		this->m_Age = age;
		this->m_Hight = hight;
		this->m_Name = name;
	}

	string m_Name;
	int m_Age;
	int m_Hight;
};

void printList(const list<Person>& L1)
{
	for (list<Person>::const_iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << "姓名：" << it->m_Name << "\t年龄：" << it->m_Age <<
			"\t身高：" << it->m_Hight << endl;
	}
}

bool myCompare(Person p1, Person p2)
{
	if (p1.m_Age != p2.m_Age)
		return p1.m_Age < p2.m_Age;
	else
		return p1.m_Hight < p2.m_Hight;
}

void test01()
{
	list<Person> L;	

	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);

	//插入数据
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);
	printList(L);

	//排序
	cout << "-------------------排序后-------------------" << endl;
	L.sort(myCompare);
	printList(L);

}

int main()
{
	test01();
	return 0;
}



